package com.swmutil.swmutil.util;

import com.swmutil.swmutil.pojo.All_Param;
import com.swmutil.swmutil.sqlite.AllParamSql;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;

import java.math.BigInteger;
import java.util.List;

public class MenuUtils {
    /**
     * 创建顶部菜单栏
     */
    public static void buildMenu() {
        StaParas.menuBar = new MenuBar();
        firstmenu();
    }

    //第一个菜单栏
    private static void firstmenu() {
        Menu fileMenu = new Menu("下发指令");

        StaParas.menuBar.getMenus().add(fileMenu);
        MenuItem sendOrderItem = new MenuItem("发送");
        fileMenu.getItems().add(sendOrderItem);
        sendOrderItem.setOnAction(e -> sendOrder());
    }
    /**
     * 退出程序
     */
    public static void sendOrder() {
            //发送的每个数据的分割
            int [] order={64,64,36,64,64,64,64,64,48,32,64,64,64,64,64,64,64,32,64,48,64,64,55,64,32,16,32,8,56,56,56,24,8,40,64,64,64};
            int orderindex=0;
            List<All_Param> allParams = AllParamSql.queryParamsList();
            String binryData = "";
            String hexData="";
            int length = 0;
            for(int i=0;i<allParams.size();i++){
                All_Param param = allParams.get(i);

                //拼接二进制
                String binaryString = Integer.toBinaryString(param.getValue());
                while (binaryString.length()< param.getBit_length()){
                    binaryString=0+binaryString;
                }

                System.out.println(binaryString);
                binryData = binryData.concat(binaryString);


                length = length +param.getBit_length();
                if(length>64){
                    System.out.println(length);
                }
                if(length==order[orderindex]){
                    orderindex++;
                    length=0;
                    BigInteger binaryNumber = new BigInteger(binryData, 2);
                    String hexString = binaryNumber.toString(16);

                    System.out.println(hexString);
                    binryData = "";
                }

                binryData = binryData.concat(binaryString);
            }




    }

    public static void main(String[] args) {

        BigInteger binaryNumber = new BigInteger("00000000000000000000000000000000", 2);
        String hexString = binaryNumber.toString(16);
        System.out.println(hexString); // 输出将会是A
    }



}
